<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let users = [
        { name: 'John', age: 20, surname: 'Johnson' },
        { name: 'Pete', age: 18, surname: 'Peterson' },
        { name: 'Ann', age: 19, surname: 'Hathaway' },
        { name: 'Jack', age: 21, surname: 'Jack chen' },
      ];
      // users.sort((a, b) => {
      //   return a.name > b.name ? 1 : -1;
      //   // return a.age > b.age ? -1 : 1;
      // });
      function byFiled(f) {
        return (a, b) => (a[f] > b[f] ? 1 : -1);
      }
      // users.sort(byFiled('name'));
      users.sort(byFiled('age'));
      console.log(users);
    </script>
  </body>
</html>
